//
//  ViewController.m
//  RecorderTest
//
//  Created by 张彦东 on 2017/5/16.
//  Copyright © 2017年 Z. All rights reserved.
//

#import "ViewController.h"
#import "ZZAudioManager.h"
#import "AudioListViewController.h"
#import "ZZAudioSettingViewController.h"
#import "ZZAudioParameter.h"

@interface ViewController ()

@property (nonatomic, weak) UIView *lineView;
@property (nonatomic, weak) UIButton *actionButton;
@property (nonatomic, weak) UIButton *stopButton;
@property (nonatomic, weak) UILabel *peak1Label;
@property (nonatomic, weak) UILabel *peak2Label;
@property (nonatomic, weak) UILabel *ave1Label;
@property (nonatomic, weak) UILabel *ave2Label;
@property (nonatomic, weak) UIProgressView *peak1Progress;
@property (nonatomic, weak) UIProgressView *peak2Progress;
@property (nonatomic, weak) UIProgressView *ave1Progress;
@property (nonatomic, weak) UIProgressView *ave2Progress;
@property (nonatomic, weak) UILabel *paramLabel;
@property (nonatomic, weak) UILabel *timeLabel;

@end

@implementation ViewController
{
    // -1:初始 0:暂停 1:运行
    int _recordStatus;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _recordStatus = -1;
    
    UIButton *action = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:action];
    _actionButton = action;
    [action addTarget:self action:@selector(actionButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *stop = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:stop];
    _stopButton = stop;
    [stop setTitle:@"stop" forState:UIControlStateNormal];
    [stop addTarget:self action:@selector(stopClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self updateButtonInfo];
    
    _peak1Label = [self addProgressLabel:@"峰值1"];
    _peak2Label = [self addProgressLabel:@"峰值2"];
    _ave1Label = [self addProgressLabel:@"平均值1"];
    _ave2Label = [self addProgressLabel:@"平均值2"];
    
    _peak1Progress = [self addProgress];
    _peak2Progress = [self addProgress];
    _ave1Progress = [self addProgress];
    _ave2Progress = [self addProgress];
    
    UILabel *parm = [[UILabel alloc] init];
    [self.view addSubview:parm];
    _paramLabel = parm;
    parm.numberOfLines = 0;
    parm.font = [UIFont systemFontOfSize:14];
    parm.textColor = [UIColor blackColor];
    
    
    UILabel *time = [[UILabel alloc] init];
    [self.view addSubview:time];
    _timeLabel = time;
    time.font = [UIFont systemFontOfSize:16];
    time.textColor = [UIColor blackColor];
    time.textAlignment = NSTextAlignmentCenter;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"History" style:UIBarButtonItemStylePlain target:self action:@selector(showList)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UIBarButtonItemStylePlain target:self action:@selector(setting)];
    
    [[ZZAudioManager manager] prepareAudio];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressChange:) name:ZZRecordMetersChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recorderStop) name:ZZRecordStopNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _paramLabel.text = [NSString stringWithFormat:
                        @"录音参数\n声道 : %@\n采样率 : %@\n比特率 : %@\n声音质量 : %@\n编码比特率 : %@\n",
                        [ZZAudioParameter channelsDisplayAtIndex:[ZZAudioManager manager].channelsIndex],
                        [ZZAudioParameter sampleRateDisplayAtIndex:[ZZAudioManager manager].sampleRateIndex],
                        [ZZAudioParameter linearPCMBitDepthDisplayAtIndex:[ZZAudioManager manager].linearPCMBitDepthIndex],
                        [ZZAudioParameter audioQualityDisplayAtIndex:[ZZAudioManager manager].audioQualityIndex],
                        [ZZAudioParameter encoderBitRateDisplayAtIndex:[ZZAudioManager manager].encoderBitRateIndex]];
}

- (void)progressChange:(NSNotification *)notification {
    [_peak1Progress setProgress:[notification.object[0] floatValue] animated:YES];
    [_peak2Progress setProgress:[notification.object[1] floatValue] animated:YES];
    [_ave1Progress setProgress:[notification.object[2] floatValue] animated:YES];
    [_ave2Progress setProgress:[notification.object[3] floatValue] animated:YES];
    _timeLabel.text = [NSString stringWithFormat:@"%lds", [notification.object[4] integerValue]];
}

- (void)recorderStop {
    [_peak1Progress setProgress:0.0f animated:YES];
    [_peak2Progress setProgress:0.0f animated:YES];
    [_ave1Progress setProgress:0.0f animated:YES];
    [_ave2Progress setProgress:0.0f animated:YES];
}

- (UILabel *)addProgressLabel:(NSString *)text {
    UILabel *label = [[UILabel alloc] init];
    [self.view addSubview:label];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:14];
    label.text = text;
    return label;
}
- (UIProgressView *)addProgress {
    UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    [self.view addSubview:progressView];
    progressView.backgroundColor = [UIColor lightGrayColor];
    progressView.progressTintColor = [UIColor blueColor];
    return progressView;
}

- (void)showList {
    AudioListViewController *alvc = [[AudioListViewController alloc] init];
    [self.navigationController pushViewController:alvc animated:YES];
}
- (void)setting {
    ZZAudioSettingViewController *setting = [[ZZAudioSettingViewController alloc] init];
    [self.navigationController pushViewController:setting animated:YES];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat viewWidth = self.view.frame.size.width;
    CGFloat viewHeight = self.view.frame.size.height;
    CGFloat buttonHeight = 200;
    _actionButton.frame = CGRectMake(0, viewHeight - buttonHeight, viewWidth / 2, buttonHeight);
    _stopButton.frame = CGRectMake(viewWidth / 2, viewHeight - buttonHeight, viewWidth / 2, buttonHeight);
    CGFloat labelWidth = 70;
    CGFloat labelHeight = 28;
    _peak1Label.frame = CGRectMake(10, viewHeight / 2, labelWidth, labelHeight);
    _peak2Label.frame = CGRectMake(10, CGRectGetMaxY(_peak1Label.frame), labelWidth, labelHeight);
    _ave1Label.frame = CGRectMake(10, CGRectGetMaxY(_peak2Label.frame), labelWidth, labelHeight);
    _ave2Label.frame = CGRectMake(10, CGRectGetMaxY(_ave1Label.frame), labelWidth, labelHeight);
    CGFloat progressWidth = viewWidth - 20 - labelWidth - 10;
    CGFloat progressHeight = 2;
    CGFloat progressYMargin = (labelHeight - progressHeight) / 2;
    CGFloat progressX = CGRectGetMaxX(_peak1Label.frame) + 10;
    _peak1Progress.frame = CGRectMake(progressX, _peak1Label.frame.origin.y + progressYMargin, progressWidth, progressHeight);
    _peak2Progress.frame = CGRectMake(progressX, _peak2Label.frame.origin.y + progressYMargin, progressWidth, progressHeight);
    _ave1Progress.frame = CGRectMake(progressX, _ave1Label.frame.origin.y + progressYMargin, progressWidth, progressHeight);
    _ave2Progress.frame = CGRectMake(progressX, _ave2Label.frame.origin.y + progressYMargin, progressWidth, progressHeight);
    NSLog(@"%@", NSStringFromCGRect(_ave2Progress.frame));
    _paramLabel.frame = CGRectMake(10, 0, viewWidth - 20, viewHeight / 2 - 30);
    _timeLabel.frame = CGRectMake(0, CGRectGetMaxY(_paramLabel.frame), viewWidth, 30);
}

- (void)stopClick {
    _recordStatus = -1;
    [self updateButtonInfo];
}

- (void)actionButtonClick {
    int statusAfterClick = -1;
    switch (_recordStatus) {
        case -1:
            statusAfterClick = 1;
            break;
        case 0:
            statusAfterClick = 1;
            break;
        case 1:
            statusAfterClick = 0;
            break;
    }
    _recordStatus = statusAfterClick;
    [self updateButtonInfo];
}

- (void)updateButtonInfo {
    NSString *actionTitle;
    switch (_recordStatus) {
        case -1:
            [[ZZAudioManager manager] recordStop];
            actionTitle = @"start";
            _timeLabel.text = @"";
            break;
        case 0:
            [[ZZAudioManager manager] recordPause];
            actionTitle = @"continue";
            break;
        case 1:
            [[ZZAudioManager manager] recordResum];
            actionTitle = @"pause";
            break;
        default:
            break;
    }
    [_actionButton setTitle:actionTitle forState:UIControlStateNormal];
    _stopButton.enabled = _recordStatus != -1;
}

@end
